package as;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Main3Serv
 */
@WebServlet("/Main3Serv")
public class Main3Serv extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Main3Serv() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String productid = request.getParameter("productId");
		String productname = request.getParameter("productName");
		String productKbnA = request.getParameter("productKbnA");
		String productKbnB = request.getParameter("productKbnB");
		String productKbnC = request.getParameter("productKbnC");
		String costPrice = request.getParameter("costPrice");
		String sellingPrice = request.getParameter("sellingPrice");
		String maker = request.getParameter("maker");

		String supplier = request.getParameter("supplier");
		String dummy1 = request.getParameter("dummy1");

		Connection conn = null;
		String url = "jdbc:mysql://localhost:3306/shangpin";
		String user = "root";
		String password = "rosy";

		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			conn = DriverManager.getConnection(url, user, password);
			Statement stmt = conn.createStatement();
			String sql = "update product set productName='"+productname+"',productKbnA='"+productKbnA+"',ProductKbnB='"+productKbnB+"',ProductKbnC='"+productKbnC+"',costPrice="+costPrice+",sellingPrice="+sellingPrice+",maker='"+maker+"',supplier='"+supplier+"',dummy1='"+dummy1+"' where productId='"+productid+"'";
			stmt.executeUpdate(sql);
			stmt.close();
			conn.close();
		} catch (ClassNotFoundException e) {
			System.out.println("Class not found");
		} catch (SQLException e) {
			System.out.println("Sql exception");
		} catch (Exception e) {
			System.out.println("exception");
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				response.getWriter().println(e.getMessage());
			}
		}
		response.sendRedirect("jsp/updatefinish.jsp");

	}

}
